package org.example.aienglishapp.service.speech.dialoguebook;

import org.example.aienglishapp.entity.speech.dialoguebook.DialogueBook;
import org.example.aienglishapp.mapper.speech.dialoguebook.DialogueBookMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Blob;
import java.util.List;

@Service
public class DialogueBookService {

    @Autowired
    private DialogueBookMapper dialogueBookMapper;

    // 获取书籍信息
    @Transactional(readOnly = true)
    public DialogueBook getBookById(Long bookId) {
        return dialogueBookMapper.findById(bookId);
    }

    // 获取所有书籍信息
    @Transactional(readOnly = true)
    public List<DialogueBook> getAllBooks() {
        return dialogueBookMapper.findAll();
    }

    // 添加书籍信息
    @Transactional(rollbackFor = {DataAccessException.class})
    public void addBook(DialogueBook dialogueBook) {
        dialogueBookMapper.insert(dialogueBook);
    }

    // 更新书籍信息
    @Transactional(rollbackFor = {DataAccessException.class})
    public void updateBook(DialogueBook dialogueBook) {
        dialogueBookMapper.update(dialogueBook);
    }

    // 删除书籍信息
    @Transactional(rollbackFor = {DataAccessException.class})
    public void deleteBook(Long bookId) {
        dialogueBookMapper.deleteById(bookId);
    }

    // 获取书籍的图片
    @Transactional(readOnly = true)
    public Blob getBookPictureById(Long bookId) {
        return dialogueBookMapper.getPictureById(bookId);
    }
}